import 'package:flutter/cupertino.dart';
import 'package:mobile_app.core/core/presentation/helpers/screen_util_mixin.dart';
import 'package:mobile_app.core/core/presentation/localization/translation.dart';
import 'package:mobile_app.core/core/presentation/theme/theme.dart';
import 'package:mobile_app.core/core/presentation/widgets/dialogs/cupertino_opaque_dialogs.dart';

class AppActionDialog extends StatelessWidget {
  const AppActionDialog({
    required this.actions,
    required this.onCancelButtonPressed,
    this.message,
    this.messageScrollController,
    this.actionScrollController,
    super.key,
  });

  final String? message;
  final List<AppActionDialogAction> actions;
  final ScrollController? messageScrollController;
  final ScrollController? actionScrollController;
  final VoidCallback onCancelButtonPressed;

  @override
  Widget build(BuildContext context) {
    final cancelButton = AppActionDialogAction(
      title: CoreTranslation.of(context).actionDialogCancelButtonTitle,
      isDefaultAction: true,
      onPressed: onCancelButtonPressed,
    );

    Widget? messageWidget;
    if (message != null) {
      messageWidget = Text(
        message!,
        style: CoreTheme.of(context).textTheme.actionDialogMessage,
      );
    }

    return CupertinoOpaqueActionSheet(
      message: messageWidget,
      actions: actions,
      cancelButton: cancelButton,
      messageScrollController: messageScrollController,
      actionScrollController: actionScrollController,
    );
  }
}

class AppActionDialogAction extends StatelessWidget {
  const AppActionDialogAction({
    required this.title,
    required this.onPressed,
    this.isDefaultAction = false,
    this.isDestructiveAction = false,
    this.showPremiumBadge = false,
    super.key,
  });

  final String title;
  final bool isDefaultAction;
  final bool isDestructiveAction;
  final bool showPremiumBadge;
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) {
    var textStyle = CoreTheme.of(context).textTheme.actionDialogAction;

    if (isDestructiveAction) {
      textStyle = textStyle.copyWith(
        color: CupertinoDynamicColor.resolve(CupertinoColors.systemRed, context),
      );
    }

    if (isDefaultAction) {
      textStyle = textStyle.copyWith(
        fontWeight: FontWeight.w600,
      );
    }

    final scaleFactor = MediaQuery.of(context).textScaleFactor;

    return CupertinoOpaqueActionSheetAction(
      isDefaultAction: isDefaultAction,
      isDestructiveAction: isDestructiveAction,
      onPressed: onPressed,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Flexible(
            child: Text(
              title,
              style: textStyle,
              textAlign: TextAlign.center,
            ),
          ),
          if (showPremiumBadge)
            Padding(
              padding: EdgeInsets.only(
                left: 8.0 * scaleFactor,
              ),
              child: const _PremiumBadge(),
            ),
        ],
      ),
    );
  }
}

class _PremiumBadge extends StatelessWidget with ScreenUtilMixin {
  const _PremiumBadge();

  @override
  Widget build(BuildContext context) {
    final scaleFactor = MediaQuery.of(context).textScaleFactor;

    return DecoratedBox(
      decoration: BoxDecoration(
        color: CoreTheme.of(context).primaryColor,
        borderRadius: BorderRadius.circular(3 * scaleFactor),
      ),
      child: Padding(
        padding: EdgeInsets.symmetric(
          horizontal: 3 * scaleFactor,
          vertical: 1 * scaleFactor,
        ),
        child: Text(
          CoreTranslation.of(context).premiumBadgeTitle,
          style: CoreTheme.of(context).textTheme.actionDialogPremiumBadge,
          textScaleFactor: scaleFactor,
        ),
      ),
    );
  }
}
